package com.yijiu.trb;

import android.graphics.PixelFormat;
import android.os.Bundle;
import android.widget.TextView;

import com.yijiu.action.BaseAction;
import com.yijiu.network.Request;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.C;
import com.yijiu.resource.CacheData;
import com.yijiu.response.RequestInvestorDetailResponse;
import com.yijiu.utils.ConvertUtils;

/**
 * 投资人详情
 * 
 * @author TangChao
 */
public class InvestorDetailActivity extends BTitleActivity
{
	private String appKey;

	private String userBaseId;

	private TextView userName, realName, phone, realNameStatus, bankCardStatus;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.RGBA_8888);
		setContentView(R.layout.investor_detail_layout);
		initWidget();
		appKey = CacheData.APP_KEY;
		userBaseId = getIntent().getStringExtra("userBaseId");
		RequestInvestorDetail(appKey, userBaseId);
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		RequestInvestorDetail(appKey, userBaseId);
	}

	@Override
	protected void initWidget()
	{
		titleView.setText("客户详情");
		userName = (TextView) findViewById(R.id.tv_investor_detail_username);
		realName = (TextView) findViewById(R.id.tv_investor_detail_realname);
		phone = (TextView) findViewById(R.id.tv_investor_detail_phone);
		realNameStatus = (TextView) findViewById(R.id.tv_investor_detail_active);
		bankCardStatus = (TextView) findViewById(R.id.tv_investor_detail_bank_card_status);
	}

	/**
	 * 客户详情请求
	 */
	private void RequestInvestorDetail(String appKey, String userBaseId)
	{
		// 将用户输入的信息拼接成字符串发送到服务器
		String inputStr = "service=appInvestorInfoService&userBaseId=" + userBaseId;
		// 用私钥进行AES加密
		encryptBase(inputStr, C.PRIVATE_KEY);
		// 将用公钥加密后的私钥、用私钥加密后的数据进行封装
		BaseAction action = new BaseAction(encodedData, encriptData, appKey);
		RequestInvestorDetailResponse requestInvestorDetailResponse = new RequestInvestorDetailResponse();
		// 请求服务器
		Request request = new Request(action, requestInvestorDetailResponse, ActionType.REQUEST_INVESTOR_DETAIL);
		netManager.excute(request, this, this);
		showProgress("正在获取投资人详情，请稍候");
	}

	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		switch (r.getType())
		{
			case ActionType.REQUEST_INVESTOR_DETAIL:
				RequestInvestorDetailResponse requestInvestorDetailResponse = (RequestInvestorDetailResponse) r.getResponse();
				userName.setText("用户名：" + requestInvestorDetailResponse.investorDetail.userName);
				realName.setText("真实姓名：" + requestInvestorDetailResponse.investorDetail.realName);
				if (requestInvestorDetailResponse.investorDetail.mobile.equals("null"))
				{
					phone.setText("手机号码:未绑定");
				}
				else
				{
					phone.setText("手机号码：" + requestInvestorDetailResponse.investorDetail.mobile);
				}
				if (!(requestInvestorDetailResponse.investorDetail.bankCardNo).equals("null"))
				{
					bankCardStatus.setText("银行卡绑定状态：已绑定");
				}
				else
				{
					bankCardStatus.setText("银行卡绑定状态：未绑定");
				}
				realNameStatus.setText("实名认证："
						+ ConvertUtils.checkRealNameStatus(requestInvestorDetailResponse.investorDetail.realNameAuthentication));
				break;
			default:
				break;
		}
	}

	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		app.showMsg(errorMsg);
	}

	@Override
	public void rePost(Request r)
	{
		super.rePost(r);
	}
}
